package com.study.opensource.spring.enable.service;

import org.springframework.stereotype.Component;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author xubangbang
 * @date 2021/8/5 13:44
 */
public class BaseServiceImpl<T> implements IBaseService<T> {

    private Class tClazz;

    public BaseServiceImpl() {
        //这里this代表的是BaseDaoImpl的子类对象
        //得到当当前类（父类）上的泛型T--父类型
        Type type = this.getClass().getGenericSuperclass();
        //得到当前类上所有的泛型类型Class
        System.out.println(type.getTypeName());
        System.out.println(type);
        Type[] types = ((ParameterizedType) type).getActualTypeArguments();
        //得到具体传入的泛型类Class对象
        tClazz = (Class) types[0];

    }

    @Override
    public List<T> list(String param) {
        try {
            T t = (T) tClazz.newInstance();
            return Stream.of(t).collect(Collectors.toList());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();

    }
}
